package test2.binarySearch;

import java.math.BigDecimal;

public class Solution2 {

    public static boolean searchMatrix(int[][] matrix, int target) {
        // 在此处写入代码
        int beginX = 0;
        int endX = matrix.length - 1;

        int beginY = 0;
        int endY = matrix[0].length -1 ;

        int y = 0;

        while (beginY <= endY) {
            y = (beginY + endY + 1) / 2;
            if (matrix[beginX][y] == target || matrix[endX][y] == target) {
                return true;
            }
            if (matrix[beginX][y] > target) {
                endY = y -1;
            } else if (matrix[endX][y] < target) {
                beginY = y + 1;
            } else {
                break;
            }
            if (endY < beginY) {
                return false;
            }
        }

        while (beginX <= endX) {
            int x = (beginX + endX + 1) / 2;
            if (matrix[x][y] > target) {
                endX = x - 1;
            } else if (matrix[x][y] < target) {
                beginX = x + 1;
            } else {
                return true;
            }
        }
        return false;

    }

    public static void main(String[] args) {
         int[][] arr = new int[][]{{1,3,5,7},{10,11,16,20},{23,30,34,50}};
         searchMatrix(arr, 3);
    }
}
